<template>
  <!-- 经理 - 转账审批 -->
  <div class="jeecg-basic-table jeecg-basic-table-form-container">
    <a-form @keyup.enter.native="searchQuery">
      <a-row :gutter="8" type="flex">
        <a-col flex="135px">
          <a-form-item label="部门" :label-col="{ style: { width: '40px' } }" :wrapper-col="{ span: 18 }">
            <JDictSelectTag
              v-model:value="queryParam.ddlxIds"
              mode="multiple"
              dictCode="ddlxType@ddlxUser"
              placeholder="全部"
              :maxTagCount="0"
              :getPopupContainer="(node) => node.parentNode"
              :dropdownStyle="{ width: '120px', overflow: 'auto' }"
              :dropdownMatchSelectWidth="false"
            />
          </a-form-item>
        </a-col>

        <a-col flex="350px">
          <a-form-item label="提交日期" :label-col="{ style: { width: '70px' } }" :wrapper-col="{ style: { width: '260px' } }">
            <a-date-picker
              v-model:value="queryParam.lrsjBegin"
              :disabled-date="disabledStartDate"
              valueFormat="YYYY-MM-DD"
              placeholder=""
              style="width: 123px"
            />
            ~
            <a-date-picker
              v-model:value="queryParam.lrsjEnd"
              :disabled-date="disabledEndDate"
              valueFormat="YYYY-MM-DD"
              placeholder=""
              style="width: 123px"
            />
          </a-form-item>
        </a-col>

        <a-col flex="120px">
          <a-form-item label="状态" :label-col="{ style: { width: '45px' } }" :wrapper-col="{ span: 17 }">
            <a-select id="stat" v-model:value="queryParam.stat" placeholder="全部" :showArrow="false" :allowClear="false">
              <a-select-option value="1">未审</a-select-option>
              <a-select-option value="2">经理</a-select-option>
              <a-select-option value="9">回退</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>

        <a-col flex="200px">
          <a-form-item label="订单号" :label-col="{ style: { width: '55px' } }" >
            <a-input placeholder="" v-model:value="queryParam.orderId" :allowClear="true"></a-input>
          </a-form-item>
        </a-col>

        <!--a-col flex="220px">
          <a-form-item label="对方户名" :label-col="{ style: { width: '70px' } }" >
            <a-input placeholder="" v-model:value="queryParam.dfhm" :allowClear="true"></a-input>
          </a-form-item>
        </a-col-->

        <a-col flex="auto" style="float:right; width:180px; ">
          <span style="float:right; overflow:hidden;" class="table-page-search-submitButtons">
            <a-button type="primary" @click="searchQuery" preIcon="ant-design:search">查询</a-button>
            <a-button type="default" @click="searchReset" preIcon="ant-design:reload-outlined" style="margin-left: 8px">重置</a-button>
          </span>
        </a-col>

      </a-row>
    </a-form>
  </div>

  <!-- table区域-begin v-fit-columns -->
  <BasicTable @register="registerTable" :onRowDbClick="handleDblclick" :onRowClick="handleClick" :rowClassName="rowClassName">
    <template #operation="{text, record}">
      <a-badge :color="statusColor(text, record)" :text="operateTim(text, record)" />
    </template>
  </BasicTable>
  <!-- table区域-end -->

  <!-- 表单区域 -->
  <AccountFlowInterval @register="registerAccountFlowInterval" @success="reload"></AccountFlowInterval>

</template>

<script lang="ts" setup name="accountFlow-payManageChecked">
import { ref, computed, unref, reactive, toRaw } from "vue";
import { BasicTable, TableAction, ActionItem, BasicColumn } from "/@/components/Table";
import { useModal } from "/@/components/Modal";
import { useListPage } from "/@/hooks/system/useListPage";
import { render } from "/@/utils/common/renderUtils";
import { payManageChecked } from "/@/views/full/account/types/accountflow.api";
import { momentSearchQueryDate } from "/@/utils/dateUtil";

import moment from 'dayjs'
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import AccountFlowInterval from './modules/AccountFlowInterval.vue';
import { darken } from "/@/utils/color";

//初始化 -------------------------------------------------------------
//定义查询表单，查询条件
const queryParam = reactive<any>({
  ddlxIds: '',
  stat: '1', //未审
  orderId: '',
  dfhm: '',
  lrsjBegin: momentSearchQueryDate('sub'),
  lrsjEnd: momentSearchQueryDate('add'),
});

//限制查询日期，开始日期不能大于结束日期
function disabledStartDate(beginDate) {
  const endDate = queryParam.lrsjEnd;
  if (!beginDate || !endDate) {
    return false;
  }
  return beginDate.valueOf() > moment(endDate).valueOf()
}

function disabledEndDate(endDate) {
  const beginDate = queryParam.lrsjBegin;
  if (!endDate || !beginDate) {
    return false;
  }
  return moment(beginDate).valueOf() > endDate.valueOf();
}
//初始化 -------------------------------------------------------------

//订单列表
const statusFmt = ({text}: { text: any }) => {
  if (!text) {
    return
  }
  const color = text == '1' ? 'green' : text == '2' ? 'red' : 'default';
  return render.renderTag(render.renderDict(text, 'accountStat'), color)
}

const sffsFmt = ({text}: { text: any }) => {
  if (!text) {
    return ''
  }
  let color = '';
  if (text == '1') {
    color = 'text-red-500'
  } else if (text == '3') {
    color = 'text-blue-500'
  } else if (text == '4') {
    color = 'text-purple-500'
  } else {
    
  }
  return render.renderColor(render.renderDict(text, 'sffsType'), color)
}

//操作人 1审核，2经理，9回退
const statusColor = (text, record) => {
  let color = '';
  if (record.stat == '1') {
    color = 'green'
  } else if(record.stat == '2') {
    color = 'magenta'
  } else if(record.stat == '9') {
    color = '#999'
  }
  if (!text) {
    color = '';
  }
  return color
}

//操作时间
const operateTim = (text, record) => {
  if (record.stat == "2") {
    text = record.shsj
  } else if (record.stat == "9") {
    text = record.spsj || record.shsj
  }
  if (!text) {
    return "";
  } else {
    return moment(text).format("YYYY-MM-DD HH:mm");
  }
}

const operator = ({ text, record }) => {
  if (record.stat == "2") {
    text = record.shrName
  } else if (record.stat == "9") {
    text = record.sprName || record.shrName
  }
  if (!text) {
    return "";
  } else {
    return text;
  }
};

//订单列表 - 表头
const columns: BasicColumn[] = [
  {title: '状态', align: 'center', dataIndex: 'stat', width: 40, customRender: statusFmt,},
  {title: '订单号', align: 'center', dataIndex: 'orderId', width: 80, className: 'text-red-500'},
  {title: '部门', align: 'center', dataIndex: 'ddlx', width: 50, ellipsis:true, customRender: ({text}) => render.renderDict(text, 'ddlxType')},
  {title: '单位名称', align: 'left', dataIndex: 'pathName', width: 150},
  {title: '方式', align: 'center', dataIndex: 'sffs', width: 35, customRender: sffsFmt,},
  {title: '支出金额', align: 'right', dataIndex: 'fkje', width: 70, className: 'text-green-500'},
  //{title: '对方户名', align: 'left', dataIndex: 'dfhm', width: 100},
  {title: '备注', align: 'left', dataIndex: 'bz', width: 180, },
  {title: '操作人', align: 'center', dataIndex: 'realname', width: 50, customRender: operator},
  {title: '操作日期', align: 'center', dataIndex: 'lrsj', width: 95, slots: { customRender: 'operation' } }
]

function searchQuery() {
  setProps({searchInfo:toRaw(queryParam)});
  reload()
}

function searchReset() {
  Object.assign(queryParam, {ddlxIds: '', ddzt: '', ywlx: ''});
  reload();
}

function handleSummary(tableData: Recordable[]) {
  const res = getRawDataSource()
  redoHeight();
  if (res) {
    return getRawDataSource().summary;
  } else {
    return [];
  }
}

// 列表页面公共参数、方法
const {prefixCls, tableContext} = useListPage({
  designScope: 'accountflow-payManageChecked',
  tableProps: {
    rowKey: 'id',
    api: payManageChecked,
    columns: columns,
    showTableSetting: false,
    showIndexColumn: true,    //行号
    clickToRowSelect: true,   //点击选择
    showActionColumn: false,
    useSearchForm: false,
    showSummary: true,
    summaryFunc: handleSummary,
    beforeFetch: (params) => Object.assign(params, unref(queryParam)),
  },
})

//注册table数据
const [registerTable, {reload, setProps, updateTableDataRecord, getRawDataSource, redoHeight}] = tableContext

//详细订单
const [registerAccountFlowInterval, {openModal: openAccountFlowInterval}] = useModal();

//单击行
let selectRows = reactive<any>({id: '', username: ''});
function handleClick(record) {
  selectRows.id = record.id
  selectRows.username = record.username
}

function rowClassName(record, index) {
  return record.id === selectRows.id ? 'row-selected' : '';
}

//双击行
function handleDblclick(record) {
  var params = Object.assign({}, queryParam)
  params['id'] = record.id
  params['type'] = 'payManageChecked'
  openAccountFlowInterval(true, params);
}
</script>
<style lang="less" scoped>

</style>
